package com.example.hrdemo.module.service;


import com.example.hrdemo.module.entity.User;
import com.example.hrdemo.module.mapper.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    //获取表内总行数
    public Integer getUserCount(){
        return userMapper.getUserCount();
    }

    //获取一个员工的信息
    public User getUserInfo(BigInteger id){
        return userMapper.getById(id);
    }

    //获取所有员工的信息
    public List<User> getAll(){
        return userMapper.getAll();
    }

    //获取所有员工的信息--分页
    public List<User> getAllPage(String keyword,Integer page,Integer pageSize){
        //偏移量的计算
        Integer offset = (page-1)*pageSize;
        return userMapper.getAllPage(keyword,pageSize,offset);
    }

    //新增一名员工
    public int creatUser(String userName,Integer userGender,String userCardNum,String birthday, String education, String birthplace,String phone,String userWorkNum,
                         String jobName,Integer wages, String workDay, String formalWorkDay,Integer workAge,Integer userStatus,String picturesUrl){
        //时间戳
        int timestamp = (int) (System.currentTimeMillis() / 1000);

        User user = new User();
        user.setUserName(userName);
        user.setUserGender(userGender);
        user.setUserCardNum(userCardNum);
        user.setBirthday(birthday);
        user.setEducation(education);
        user.setBirthplace(birthplace);
        user.setPhone(phone);
        user.setUserWorkNum(userWorkNum);
        user.setJobName(jobName);
        user.setWages(wages);
        user.setWorkDay(workDay);
        user.setFormalWorkDay(formalWorkDay);
        user.setWorkAge(workAge);
        user.setUserStatus(userStatus);
        user.setPicturesUrl(picturesUrl);
        user.setCreateTime(timestamp);
        user.setUpdateTime(timestamp);
        user.setIsDeleted(0);
        return userMapper.insert(user);
    }

    //修改员工信息
    public int updateUser(BigInteger userId, String userName,Integer userGender,String userCardNum,String birthday, String education, String birthplace,String phone,
                          String userWorkNum,String jobName,Integer wages, String workDay, String formalWorkDay,Integer workAge,Integer userStatus,String picturesUrl){
        //时间戳
        int timestamp = (int) (System.currentTimeMillis() / 1000);

        User user = new User();
        user.setId(userId);
        user.setUserName(userName);
        user.setUserGender(userGender);
        user.setUserCardNum(userCardNum);
        user.setBirthday(birthday);
        user.setEducation(education);
        user.setBirthplace(birthplace);
        user.setPhone(phone);
        user.setUserWorkNum(userWorkNum);
        user.setJobName(jobName);
        user.setWages(wages);
        user.setWorkDay(workDay);
        user.setFormalWorkDay(formalWorkDay);
        user.setWorkAge(workAge);
        user.setUserStatus(userStatus);
        user.setPicturesUrl(picturesUrl);
        user.setUpdateTime(timestamp);

        return userMapper.update(user);
    }

    //删除一名员工
    public int deleteUser(BigInteger id){
        //时间戳
        int timestamp = (int) (System.currentTimeMillis() / 1000);
        return userMapper.delete(id,timestamp);
    }


}
